package com.wans.easyuibackend.service.impl;

import com.wans.easyuibackend.exception.data.ExcelFormatException;
import com.wans.easyuibackend.mapper.StaffMapper;
import com.wans.easyuibackend.param.response.ExcelStaffInfo;
import com.wans.easyuibackend.pojo.Result;
import com.wans.easyuibackend.pojo.StaffInfo;
import com.wans.easyuibackend.service.ExcelService;
import com.wans.easyuibackend.utils.data_assembly.ExcelStaffAssembly;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.poi.ss.usermodel.*;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;

@Service
@Slf4j
@SuppressWarnings({"all"})
public class ExcelServiceImpl implements ExcelService {

    @Autowired
    private StaffMapper staffMapper;
    @Autowired
    private ExcelStaffAssembly excelStaffAssembly;

    @Override
    public Object findExcelStaff(HttpServletResponse response) {
        //Workbook workbook = new XSSFWorkbook();
        // 报错
        Workbook workbook = null;
        Sheet sheet = workbook.createSheet("员工信息一览表");

        // 创建表头
        Row headerRow = sheet.createRow(0);
        String[] headers = {"姓名", "姓名全拼", "性别", "证件类型", "证件号码", "出生日期", "手机号", "电子邮箱"};
        for (int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
        }

        // 填充数据
        ArrayList<StaffInfo> staffInfos = staffMapper.findStaffInfo(0, Integer.MAX_VALUE);
        for (int i = 0; i < staffInfos.size(); i++) {
            Row row = sheet.createRow(i + 1);
            // 假设数据已经按照列名映射好
            row.createCell(0).setCellValue(staffInfos.get(i).getStaffName().toString());
            row.createCell(1).setCellValue(staffInfos.get(i).getStaffNameSpelling().toString());
            row.createCell(2).setCellValue(staffInfos.get(i).getStaffGender().toString());
            row.createCell(3).setCellValue(staffInfos.get(i).getStaffIdentityType().toString());
            row.createCell(4).setCellValue(staffInfos.get(i).getStaffIdentity().toString());
            row.createCell(5).setCellValue(staffInfos.get(i).getStaffBirthday().toString());
            row.createCell(6).setCellValue(staffInfos.get(i).getStaffPhoneNumber().toString());
            row.createCell(7).setCellValue(staffInfos.get(i).getStaffEmail().toString());
            // 其他列类似处理
        }

        // 写入到响应中
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=staffInfo.xlsx");
        try {
            workbook.write(response.getOutputStream());
            workbook.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
        }
        return null;
    }

    @Override
    public Object findExcelTemplate(HttpServletResponse response) {
       // Workbook workbook = new XSSFWorkbook();
        // 报错
        Workbook workbook = null;
        Sheet sheet = workbook.createSheet("员工信息一览表");

        // 创建表头
        Row headerRow = sheet.createRow(0);
        String[] headers = {"姓名", "姓名全拼", "性别", "证件类型", "证件号码", "出生日期", "手机号", "电子邮箱"};
        for (int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
        }

        // 写入到响应中
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=staffInfo-Template.xlsx");
        try {
            workbook.write(response.getOutputStream());
            workbook.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
        }

        return null;
    }

    @SneakyThrows  //偷偷抛出异常 不显示
    @Override
    @Transactional
    public Object addExcelStaff(MultipartFile staffInfo) {

        //数据组装
        ArrayList<ExcelStaffInfo> staffInfos = excelStaffAssembly.ExcelStaffAssembly(staffInfo);

        //数据插入
        ArrayList<String> errorMsg = excelStaffAssembly.ImprotStaffInfo(staffInfos);

        if (!errorMsg.isEmpty()) {
            throw new ExcelFormatException("Excel格式错误", errorMsg);
        }

        return Result.success();
    }
}
